package com.androidbook.garbagedemo1.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.androidbook.garbagedemo1.R;
import com.androidbook.garbagedemo1.adapter.MyPagerAdapter;
import com.androidbook.garbagedemo1.entity.TabEntity;
import com.androidbook.garbagedemo1.fragment.Fragment1;
import com.androidbook.garbagedemo1.fragment.Fragment2;
import com.androidbook.garbagedemo1.fragment.Fragment3;
import com.androidbook.garbagedemo1.fragment.Fragment4;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

public class ReadActivity extends AppCompatActivity {
    private String[] mTitles = {"可回收物", "干垃圾", "湿垃圾", "有害垃圾"};
    private int[] mIconUnselectIds = {
            R.mipmap.trash1, R.mipmap.trash2,
            R.mipmap.trash3, R.mipmap.trash4};
    private int[] mIconSelectIds = {
            R.mipmap.trash1, R.mipmap.trash2,
            R.mipmap.trash3, R.mipmap.trash4};

    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        viewPager = findViewById(R.id.viewpager);
        commonTabLayout = findViewById(R.id.commonTabLayout);
        mFragments.add(Fragment1.newInstance());
        mFragments.add(Fragment2.newInstance());
        mFragments.add(Fragment3.newInstance());
        mFragments.add(Fragment4.newInstance());

        //底部导航栏
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
    }
    }

//    @Override
//    protected int initLayout() {
//        return R.layout.activity_read;
//    }
//
//    @Override
//    protected void initView() {
//        viewPager = findViewById(R.id.viewpager);
//        commonTabLayout = findViewById(R.id.commonTabLayout);
//    }
//
//    @Override
//    protected void initData() {
//        mFragments.add(Fragment1.newInstance());
//        mFragments.add(Fragment2.newInstance());
//        mFragments.add(Fragment3.newInstance());
//        mFragments.add(Fragment4.newInstance());
//
//        //底部导航栏
//        for (int i = 0; i < mTitles.length; i++) {
//            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
//        }
//        commonTabLayout.setTabData(mTabEntities);
//        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
//            @Override
//            public void onTabSelect(int position) {
//                viewPager.setCurrentItem(position);
//            }
//
//            @Override
//            public void onTabReselect(int position) {
//            }
//        });
//        viewPager.setOffscreenPageLimit(mFragments.size());
//        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                commonTabLayout.setCurrentTab(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
//        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));
//    }
//}
